package org.NooLab.texx.properties;


import java.io.File;

import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.strings.ArrUtilities;



import org.NooLab.texx.TexxFactory;
import org.NooLab.texx.TexxPluginSettings;

import org.NooLab.texx.components.AlgorithmDeclarationsLoader;
import org.NooLab.texx.storage.ContainerStorageDevice;
import org.NooLab.texx.storage.FileOrganizer;


public class TexxProperties 
								extends 
											TexxAppPropertiesAbstract 
								implements 	// 
											TexxAppGeneralPropertiesIntf{

	
	public static final int _TXX_DISPLAY_PROGRESS_BASIC = 1;

	public static final int _TXX_DISPLAY_PROGRESS_STEPS = 2;

	public static final int _SRC_TYPE_FILE = 0;

	public static final int _WORKER_TYPE_DEFAULT = 0;

	public static final String _STORAGE_OBJ = "Texx.properties";
	public static final String _STORAGE_XML = "Texx-properties.xml";

	public static final String _APP_CONTEXT_ALONE = "standalone";
	public static final String _APP_CONTEXT_ITEXX = "itexx";
	
	
	static private SettingsTransporter settingsTransporter;
	private static TexxProperties sfp;

	
	private boolean messagingActive;
	private boolean multithreadedProcesses;
	private boolean initializationOK;
	// private TexxFactory texxFactory;
	// private String algorithmsConfigPath;

	TexxControlProperties controlSettings;
	
	private String currentSettingsXml;
	private int glueType;
	private int sourceType;
	



	private boolean isPluginsAllowed;

	private int absoluteRecordLimit;

	private OutputSettings outputSettings;

	private boolean pluginsAvailable;

	private String applicationContext = "";
	
	
	// ========================================================================
	public TexxProperties(){
		super();
		
		controlSettings = new TexxControlProperties(this); 
	}
	// ========================================================================

	
	// ------------------------------------------------------------------------
	public String getPluginsConfigPath() {

		return null;
	}
	// ------------------------------------------------------------------------

	
	

	public boolean initializationOK() {
		return initializationOK;
	}

	public boolean getInitializationOK() {
		return initializationOK;
	}
	public boolean isInitializationOK() {
		return initializationOK;
	}
	public void setInitializationOK(boolean initializationOK) {
		this.initializationOK = initializationOK;
	}
	
	
	
	public void setFactoryParent(TexxFactory factory) {
		texxFactory = factory;
	}
	
	public TexxFactory getSfFactory() {
		return texxFactory;
	}
	
	/**
	 * refers to "builtinscatalog.xml" which is necessary for global indexing and activation of built-in algorithms;
	 * if it does not exist (path or file) only the most basic algorithms are available
	 * 
	 * @param pathstring
	 */
	public void setAlgorithmsConfigPath(String pathstring) {
		// 
		
		String ps ;
		ps = DFutils.createPath( pathstring,"/");
		if (pathstring==null){
			return;
		}
		if (pathstring.endsWith("/")==false){
			pathstring = pathstring+"/" ; 
		}

		algorithmsConfigPath = pathstring;
	}


	public String getAlgorithmsConfigPath() {
		return algorithmsConfigPath;
	}


	
	// ------------------------------------------------------------------------
	

	public AlgorithmDeclarationsLoader getAlgoDeclarations() {
		return algoDeclarations;
	}


	public void setAlgoDeclarations(AlgorithmDeclarationsLoader algoDeclarations) {
		this.algoDeclarations = algoDeclarations;
	}

	public void setMessagingActive(boolean flag) {
		 
		messagingActive = flag;
	}


	public boolean getMessagingActive() {
		 
		return messagingActive;
	}


	public void activateMultithreadedProcesses( boolean flag) {
		// 
		multithreadedProcesses = flag;
	}
	public boolean isMultithreadedProcesses() {
		return multithreadedProcesses;
	}
	public boolean getMultithreadedProcessing() {
		return multithreadedProcesses;
	}
	public void setMultithreadedProcesses(boolean flag) {
		this.multithreadedProcesses = flag;
	}
	
	
	public TexxProperties(TexxProperties appProperties) {
		  
		sfp = this;
		
	}


	public static TexxProperties getInstance() {
		 
		return getInstance("");
	}
	/**   */
	public static TexxProperties getInstance( String xmlSettingsStack ) {
	 
		sfp = new TexxProperties();
		
		if (xmlSettingsStack.length()>0){
			settingsTransporter = new SettingsTransporter( sfp );
			sfp = settingsTransporter.importProperties( xmlSettingsStack );
		}
		 
		return sfp;
	}

	
	public void importSettings(){
		
	}

	
	public String exportSettings(){
		String xmlSettingsStr = "";
		
		settingsTransporter = new SettingsTransporter( sfp );
		// target directory is given by sfp itself...
		xmlSettingsStr = settingsTransporter.exportProperties(0); // 0
		
		currentSettingsXml = xmlSettingsStr;
		
		return xmlSettingsStr;
	}



	/**
	 * 
	 * @param sourceType 1=full data file, 5=profiles for simulation
	 * @param filename
	 */
	public void setDataSource(int sourcetype, String locatorname) {
		// 
		this.sourceType = sourcetype;
		dataSrcFilename = locatorname ; 
	}

	/**   */
	public boolean addDataSource(int sourcetype, String locatorname) {
		boolean result=false;
		String rootpath =""; // e.g.  "D:/data/projects/"
		String prjname = "", filename="",relPath;
		
		sourceType = sourcetype;
		locatorname = locatorname.trim();
		
		if ((locatorname.indexOf("/")==0) || (DFutils.fileExists(locatorname)==false)){
			
			rootpath = persistenceSettings.getPathToSomFluidSystemRootDir();
			
			prjname = persistenceSettings.getProjectName() ;
			relPath = prjname+"/data/raw/";
			relPath = relPath.replace("//", "/").trim();
			
			filename = DFutils.createPath(rootpath.trim(), relPath);
			
			// "D:/data/projects/bank2/data/raw/bankn_d2.txt" 
			locatorname = DFutils.createPath( filename,locatorname );
			// e.g.  "D:/data/projects/bank2/data/raw/bankn_d2.txt"
		}
		dataSrcFilename = locatorname ; 
		
		result = DFutils.fileExists(dataSrcFilename) ;
		return result;
	}

	 

	public void setDataSrcFilename(String dataSrcFilename) {
		this.dataSrcFilename = dataSrcFilename;
	}


	public int getSourceType() {
		return sourceType;
	}


	public void setSourceType(int sourceType) {
		this.sourceType = sourceType;
	}



	public int getGlueType() {
		return glueType;
	}



	public void setGlueType(int glueType) {
		this.glueType = glueType;
	}

	public void setGlueInstanceType(int glueinstanceType) {
		glueType = glueinstanceType;
	}
	


	public void setPathToSomFluidSystemRootDir(String dir) {
		// 
		systemRootDir = dir;
		persistenceSettings.setPathToSomFluidSystemRootDir(dir);
	}


	public String getSystemRootDir() {
		if (systemRootDir.length()<3){
			systemRootDir = (new File(systemRootDir)).getAbsolutePath() ;
		}
		return systemRootDir;
	}

	public void setSystemRootDir(String systemRootDir) {
		this.systemRootDir = systemRootDir;
	}
 
	
	public int getMultiProcessingLevel() {
		return multiProcessingLevel;
	}
	public void setMultiProcessingLevel(int mppLevel) {
		multiProcessingLevel = mppLevel;
	}


	public boolean isPluginsAllowed() {
		return isPluginsAllowed;
	}


	public void setPluginsAllowed(boolean isPluginsAllowed) {
		this.isPluginsAllowed = isPluginsAllowed;
	}


	public void setAbsoluteRecordLimit(int value) {
		
		absoluteRecordLimit = value;
	}


	public void setglueModuleMode(int i) {
		// TODO Auto-generated method stub
		
	}


	public OutputSettings getOutputSettings() {
		return outputSettings;
	}


	public void setShowSomProgress(int ctrlValue) {
		// 
		
	}


	public PersistenceSettings getPersistenceSettings() {
		// 
		return persistenceSettings;
	}
	public void setPersistenceSettings(PersistenceSettings ps) {
		// 
		persistenceSettings = ps;
	}
	@Override
	public FileOrganizer getFileOrganizer() {
		// 
		return fileOrganizer;
	}

	public void setInstanceType() {

		fileOrganizer = new FileOrganizer();
		persistenceSettings = new PersistenceSettings(fileOrganizer);
		outputSettings = new OutputSettings(persistenceSettings);

		fileOrganizer.setPropertiesBase(this);

	}


	public void save() {
		// save(0); serializing is missing .. 
	}
	
	
	public void save(int target) {
		
		String dir,  propertiesFileName;
		
		FileOrganizer fileorg = new FileOrganizer ();
		fileorg.setPropertiesBase(this);
		
		DFutils fileutil = new DFutils();
		
		if (target>=1){
			
			dir = fileutil.getUserDir();
			propertiesFileName = DFutils.createPath( dir, TexxProperties._STORAGE_XML ) ;
			String xstr = exportSettings();
			fileutil.writeFileSimple(propertiesFileName,xstr);
			
		}else{
			dir = fileorg.getObjectStoreDir();
			propertiesFileName = DFutils.createPath( dir, TexxProperties._STORAGE_OBJ ) ;
			
			// now loading the desired properties into a new object;
			ContainerStorageDevice storageDevice ;
			storageDevice = new ContainerStorageDevice();
			
			storageDevice.storeObject( this,propertiesFileName) ;

			DFutils.reduceFileFolderList( dir,1,".properties",20) ;
		}
	}


	public TexxControlProperties getControlProperties() {
		return controlSettings;
	}

	public void setPluginsAvailable(boolean flag) {

		pluginsAvailable = flag;		
	}
	public boolean getPluginsAvailable() {

		return pluginsAvailable ;		
	}
	public boolean isPluginsAvailable() {
		return pluginsAvailable;
	}


	public String getApplicationContext() {
		 
		return applicationContext;
	}


	public void setApplicationContext(String appContext) {
		this.applicationContext = appContext;
	}


	public void setTikaPorts(int[] tikaports) {
		tikaPorts = ArrUtilities.copyArray(tikaports);
	}

	public int[] getTikaPorts() {
		return tikaPorts ;
	}




	
}
